export default (questions, answer) => {
	const qmap = new Map()
	questions.forEach(q => qmap.set(q.uuid, {question: q, answer: answer(q.uuid)}))

	const parse_radio = (item, target) => {
		return (item.mask.indexOf(target.answer) != -1) ^ item.inverted
	}

	const parse_checkbox = (item, target) => {
		const answer = target.answer || []
		if (item.inverted) {
			return item.mask
				.map(i => answer.indexOf(i) == -1)
				.reduce((x, y) => x && y, true)
		} else {
			return item.mask
				.map(i => answer.indexOf(i) != -1)
				.reduce((x, y) => x || y, false)
		}
	}

	const calc_once = item => {
		const target = qmap.get(item.uuid)
		const parser = target.question.type === "checkbox" ? parse_checkbox : parse_radio
		return parser(item, target)
	}

	const calc = q => {
		if (!q.wants.length) {
			return true
		}
		const combinator = q.interlock_combinator === "and" ?
			(x, y) => (x && y) : (x, y) => (x || y)
		const initial = q.interlock_combinator === "and"

		return q.wants.map(i => calc_once(i)).reduce(combinator, initial)
	}

	var ret = {}
	questions.forEach(q => ret[q.uuid] = !calc(q))
	return ret
}
